﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Script.Serialization;
using RoadTeam.Standard;
using RoadTeam.BLL;
using RoadTeam.Pub;
using RoadTeam.Enums;
using System.Threading;


public partial class 对接测试页面_ImgToJson : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        byte[] bits = File.ReadAllBytes(Server.MapPath(@"~/Pic/x0bqE - Imgur.jpg"));
        TextBox1.Text = Convert.ToBase64String(bits);

        RoadTeam.Standard.CsMaintenanceFile file = new RoadTeam.Standard.CsMaintenanceFile();
        file.ImgName = @"x0bqE - Imgur.jpg";
        file.ImgType = @"维修单";
        file.BusninessType = @"维修单";
        file.BusninessIdentitfier = CreateIdentifier.GetIdentifier();
        file.StaffName = @"张三";

        JavaScriptSerializer jvs = new JavaScriptSerializer();
        TextBox2.Text = jvs.Serialize(file);

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        String Json = TextBox2.Text;
        String Image = TextBox1.Text;

        byte[] ImageByte = Convert.FromBase64String(Image);
        JavaScriptSerializer jvs = new JavaScriptSerializer();
        RoadTeam.Standard.CsMaintenanceFile fileInfo = jvs.Deserialize<RoadTeam.Standard.CsMaintenanceFile>(Json);

        String RtSysPath = new RtParameter(RoadTeam.Enums.ParaName.AppFilePath).ParamValue;

        String PathDate = DateTime.Now.Year.ToString() + ((DateTime.Now.Month.ToString().Length == 2) ? DateTime.Now.Month.ToString() : "0" +
          DateTime.Now.Month.ToString()) + DateTime.Now.Day.ToString();
        String Paths = GlobalConfig.FilePath + "\\" + PathDate;
        RtSysPath = RtSysPath + "/" + PathDate;

        if (!Directory.Exists(Paths + "\\" + fileInfo.StaffName))//判断是否存在        
            Directory.CreateDirectory(Paths + "\\" + fileInfo.StaffName);//创建新路径

        Paths = Paths + @"\" + fileInfo.StaffName;
        RtSysPath = RtSysPath + @"/" + fileInfo.StaffName;

        try
        {
            using (FileStream fs = new FileStream(Paths + "\\" + fileInfo.ImgName, FileMode.Create))
            {
                fs.Write(ImageByte, 0, ImageByte.Count());
                fs.Flush();
                fs.Close();
                fs.Dispose();
            }
        }
        catch 
        {
           // return false;
        }

        CsMaintenanceFileBll bll = new CsMaintenanceFileBll();
        bll.InsertFileInfo(fileInfo.BusninessIdentitfier, Paths, RtSysPath, fileInfo.ImgName, fileInfo.ImgType, fileInfo.BusninessType);
    }
}